package com.hdax.dmw.user.service.impl;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.MD5;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdax.dmw.dtos.user.LoginDto;
import com.hdax.dmw.entity.user.DmUser;
import com.hdax.dmw.exceptions.user.LoginException;
import com.hdax.dmw.user.service.DmUserService;
import com.hdax.dmw.user.mappers.DmUserMapper;
import com.hdax.dmw.utils.Result;
import com.hdax.dmw.utils.ResultGenerator;
import com.hdax.dmw.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
* @author guoyisha
* @description 针对表【dm_user(用户表)】的数据库操作Service实现
* @createDate 2023-06-15 12:19:00
*/
@Service
public class DmUserServiceImpl extends ServiceImpl<DmUserMapper, DmUser>
    implements DmUserService{

    @Autowired
    private RedisTemplate redisTemplate;

    private LoginDto getDto(DmUser user){
        LoginDto dto = new LoginDto();
        dto.setNickName(user.getNickName());
        Map<String,Object> params =new HashMap<>();
        params.put("account",user.getRealName());
        params.put("nickName",user.getNickName());
        params.put("id",user.getId().toString());
        String token = TokenUtil.createToken(params);
        long currentTimeMillis = System.currentTimeMillis();
        dto.setToken(token);
        dto.setGenTime(currentTimeMillis);
        dto.setExtTime(dto.getGenTime() + TokenUtil.TOKEN_EXPIRE_TIME);
        //将用户信息存储在redis hash中
        Map<String,Object> redisMap = new HashMap<>();
        redisMap.put("userId",user.getId());
        redisMap.put("genTime",currentTimeMillis);
        redisMap.put("expTime",dto.getGenTime() + TokenUtil.TOKEN_EXPIRE_TIME * 2);
        redisMap.put("token",token);
        //token md5
        String s = SecureUtil.md5(token);
        dto.setMd5(s);
        redisMap.put("md5",s);
        redisTemplate.opsForHash().putAll("token:"+s,redisMap);
        redisTemplate.expire("token:"+s,TokenUtil.TOKEN_EXPIRE_TIME * 2, TimeUnit.MILLISECONDS);
        return dto;
    }

    @Override
    public Result<LoginDto> login(String phone, String password) {
        QueryWrapper<DmUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        DmUser user = getOne(queryWrapper);
        if(user == null){
            //1006 手机号不正确或者不存在
            throw new LoginException("1006","手机号不正确或者不存在");
        }else {
            if(!user.getPassword().equals(SecureUtil.md5(password))){
                throw new LoginException("1007","密码不正确");
            }
        }
        LoginDto dto = getDto(user);
        return ResultGenerator.genSuccessResult(dto);
    }

    @Override
    public Result<LoginDto> refreshToken(String md5) {
        Long userId = Long.parseLong(redisTemplate.opsForHash().get("token:" + md5, "userId").toString());
        DmUser user = getById(userId);
        LoginDto dto = getDto(user);
        return ResultGenerator.genSuccessResult(dto);
    }
}




